package com.google.code.pdfbrick.splitter;

import com.google.code.pdfbrick.engine.command.FilePageRangeParser;
import com.google.code.pdfbrick.engine.constant.CommandConstant;
import com.google.code.pdfbrick.engine.exception.CommandParseException;

/**
 * @author Johny Huang
 * @date 2009-3-13
 */
public class SplitCommandParser {

	/**
	 * <p>The split command should has a pattern like <br/><br/>
	 * -split srcFilePath [desFilePath&lt;PageRange,PageRange...&gt;;desFilePath&lt;&gt;;...]
	 * <br/><br/>
	 * The PageRange references to the pages of source file.
	 * </p>
	 * <p>PageRange pattern example:
	 * <9,5-14,7-3><br/>
	 * 9 means the page 9.It's a PageRange of single page.<br/>
	 * 5-14 means from page 5(included) to page 14.It's a PageRange in order.<br/>
	 * 7-3 means from page 7(included) to page 3.It's a PageRange in reverse order.<br/>
	 * </p>
	 * 
	 * @param A String array which is split from a command line.
	 * @return
	 */
	public SplitCommandParams parse(final String[] args){
		if (args == null) {
			throw new NullPointerException("The param of the command should not be null!");
		}

		if (args.length < 2) {
			throw new CommandParseException("The number of the command params should be two!");
		}

		SplitCommandParams params = new SplitCommandParams();
		final String srcFilePath = args[0];
		final String desFilePaths = args[1];
		final String[] desFilePathArray = desFilePaths
				.split(CommandConstant.SEMICOLON);

		params.setSrcFilePath(srcFilePath);
		FilePageRangeParser fprParser=FilePageRangeParser.getInstance();
		for (String filePath : desFilePathArray) {
			params.addFilePageRange(fprParser
					.parseFilePageRangeStr(filePath));
		}
		
		return params;
	}
}
